package com.gopay.scheduler.job;

import com.gopay.common.constants.proccode.ProcCodeConstants;
import com.gopay.common.constants.trans.*;
import com.gopay.common.constants.txncd.IntTxnCd;
import com.gopay.common.cps.dao.order.CpsGeneralMainOrderQueryDAO;
import com.gopay.common.cps.dao.order.CpsTran00700AssistDAO;
import com.gopay.common.cps.dao.order.CpsTransCloud00800AssistDAO;
import com.gopay.common.domain.cps.CpsTrans00700Assist;
import com.gopay.common.domain.cps.CpsTransCloud00800Assist;
import com.gopay.common.domain.cps.cloud.CloudFreeDrawVO;
import com.gopay.common.domain.cps.transfer.Tran00700AsyncVO;
import com.gopay.common.domain.cps.vo.CPSResponse;
import com.gopay.common.exception.GopayException;
import com.gopay.remote.order.MainOrderUpdateRemoteService;
import com.gopay.scheduler.GopayBaseJob;
import com.gopay.scheduler.common.CallCPSHelper;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import javax.annotation.Resource;
import java.util.*;

/**
 *
 * 云账户免密提现 00800 异步消费
 * @author sh.jiao
 * @TransCloud00800AsyncJob
 * @2017/12/22 16:27  www.gopay.com.cn Inc.All rights reserved.
 */

public class TransCloud00800AsyncJob extends GopayBaseJob {

    private Logger logger = LoggerFactory.getLogger(TransCloud00800AsyncJob.class);
    private CpsGeneralMainOrderQueryDAO cpsGeneralMainOrderDAO;
    private MainOrderUpdateRemoteService cpsGeneralMainOrderOperClient;

    private static final int QUERYCOUNT = 200;


    private static final int txnStaCd = TxnStaCode.TXN_STA_CD_30000.value;
    private static final String gopayIntTxnCd = IntTxnCd._00800.value;

    @Resource(name = "cpsGeneralMainOrderOperClient")
    public void setCpsGeneralMainOrderOperClient(MainOrderUpdateRemoteService cpsGeneralMainOrderOperClient) {
        this.cpsGeneralMainOrderOperClient = cpsGeneralMainOrderOperClient;
    }

    @Resource(name = "cpsGeneralMainOrderDAO")
    public void setCpsGeneralMainOrderDAO(CpsGeneralMainOrderQueryDAO cpsGeneralMainOrderDAO) {
        this.cpsGeneralMainOrderDAO = cpsGeneralMainOrderDAO;
    }


    @Resource(name = "cpsTransCloud00800AssistDAO")
    private CpsTransCloud00800AssistDAO cpsTransCloud00800AssistDAO;

    @Resource(name = "callCPSHelper")
    private CallCPSHelper callCPSHelper;

    @Override
    protected void doJobInternal() {

        long startMillions = System.currentTimeMillis();
        logger.info("云账户免密提现 第一阶段账务 异步处理开始 ");

        String sql = "from CpsTransCloud00800Assist t where " +
                " t.status= '" + TransCloud00800AssistStatus.INIT.value +"'" +
                " and t.createdTs >= :date1" +
                " and t.createdTs <= :date2";

        Map<String, Object> map = new HashMap();
        //map.put("status1", Trans00700AssistStatus.INIT.value);


        //date1 多久时间内的数据
        Date date = new Date();
        Calendar calendar1 = Calendar.getInstance();
        calendar1.setTime(date);
        calendar1.add(Calendar.DAY_OF_YEAR, -1);  //减一天
        logger.error("date1:"+calendar1.getTime());
        map.put("date1", calendar1.getTime());
        Calendar calendar2 = Calendar.getInstance();
        calendar2.setTime(date);
        // modify by sh.jiao bug修改这里把MILLISECOND改成SECOND
        //calendar2.add(Calendar.MILLISECOND, -5);  //减五秒
        calendar2.add(Calendar.SECOND, -5);  //减五秒
        logger.error("date2:"+calendar2.getTime());
        map.put("date2", calendar2.getTime());


        List<CpsTransCloud00800Assist> assists = cpsTransCloud00800AssistDAO.findByHql(sql, map, 1, QUERYCOUNT);

        callCps(assists);

        long endMiilions = System.currentTimeMillis();
        long result = (endMiilions - startMillions) / 1000;
        logger.info("云账户免密提现 第一阶段账务 异步处理结束 耗费=" + result + "秒");

    }

    private void callCps(List<CpsTransCloud00800Assist> assists)  {

        if (!(assists.isEmpty())) {
            logger.info("取到待处理数据条数：" + assists.size());
            for (CpsTransCloud00800Assist order : assists) {
                String gopayOrderId = order.getGopayOrderId();
                try {

                    logger.info("处理数据开始gopayOrderid=" + gopayOrderId);

                    CloudFreeDrawVO vo = new CloudFreeDrawVO();
                    vo.setGopayOrderId(gopayOrderId);
                    vo.setOpType(DrawOpType.APPLY_CONFIRM);
                    //调用cps
                    CPSResponse cpsResponse = callCPSHelper.callByVOTicket(vo);
                    String respCode = cpsResponse.getRespCode();

                    if (!StringUtils.equals(ProcCodeConstants.PROC_CODE_100S1000, respCode)) {
                        logger.error("云账户免密提现 第一阶段账务 调用cps返回码异常respCode={}, gopayOrderId={}", respCode, gopayOrderId);
                    } else {
                        logger.info("处理cps 调用 成功gopayOrderid=" + gopayOrderId);
                    }

                } catch (GopayException e) {
                    logger.error("调用cps异常gopayOrderId=" + gopayOrderId , e);

                } catch (Exception e) {
                    logger.error("调用cps异常gopayOrderId=" + gopayOrderId , e);

                }
            }
        } else {
            logger.info("云账户免密提现 第一阶段账务 取回列表为空...");
        }
    }
}
